// pages/cancellation/cancellation.js
import { orderVerific } from "../../api/order";
var app = getApp();
Page({

   /**
    * 页面的初始数据
    */
   data: {
      parameter: {
         'navbar': '1',
         'return': '1',
         'title': '订单核销',
         'color': true,
         'class': '0'
       },
       showDialog: false,
       number:''
   },
   number:function(e){
      this.setData({
         number: e.detail.value
         })
   },
   toggleDialog(e) {
      var that = this
      let ref = /[0-9]{12}/;
         if (that.data.number == "" || that.data.number == null) return app.Tips({title:'请输入核销码！'});
         if (!ref.test(that.data.number)) return app.Tips({title:'请输入正确的核销码！'});
         orderVerific(that.data.number, 0)
         .then(res => {
            if(res.status == '200'){
                  this.setData({
                     showDialog: !this.data.showDialog,
                     image:res.data.image,
                     order_id:res.data.order_id
                  });
            }
         })
         .catch(res => {
           return app.Tips({title:"核销失败！"});
           that.data.number = "";
         });
    },
    confirm: function () {
      var that = this
      orderVerific(that.data.number, 1)
        .then(res => {
          wx.showToast({
            title: '核销成功！', // 标题
            icon: 'success',  // 图标类型，默认success
            duration: 1500  // 提示窗停留时间，默认1500ms
          })
          this.setData({
            showDialog: !this.data.showDialog
          });
        })
        .catch(res => {
          return app.Tips({ title: res.msg });
        });

      },
      no:function(){
      this.setData({
        showDialog: !this.data.showDialog
      });
      },
      click:function(){
         wx.scanCode({
            success: (res) => {
            console.log(res);
            var code = res.result
            orderVerific(code, 0)
         .then(res => {
            if(res.status == '200'){
                  this.setData({
                     showDialog: !this.data.showDialog,
                     image:res.data.image,
                     order_id:res.data.order_id,
                      number: code
                  });
            }
         })
         .catch(res => {
           return app.Tips({title:"核销失败！"});
           that.data.number = "";
         });
            },
            fail: (res) => {
            console.log(res);
            }
           })
          
      },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {

   },
   whiteBg:function(){
         
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   }
})